package com.serverconnection;

import java.io.IOException;

public class HTTPMethods {
private static String SERVER_SCRIPTS_URL="http://10.181.16.38/cgi-bin/";
private static String CONNECTION_ERROR="<error>\n<msg>Error Connecting to Database</msg>\n</error>";

	
	/**
	 * Logs a user into the system via the 'sitekey.pl' script.
	 * Given a valid login, returns a randomly generated 'key' and the 'user_id' (in XML form)
	 * which must be used in future calls to access and modify shipment data.
	 * 
	 * This is a POST method, so be careful of duplicate calls, they CAN and WILL alter state.
	 * 
	 * @param user the username of the party who wants to view/receive shipments
	 * @param password the password of the party who wants to view/receive shipments
	 * @return  the XML output of 'sitekey.pl' as a String
	 */
	public static String getSitekey(String user, String password)
	{	
		String data = "u="+user+"&p="+password;
		HTTPData result = new HTTPData();
		
		try {
			result = HTTPRequest.post(SERVER_SCRIPTS_URL+"sitekey.pl", data);
		}
		catch (IOException e) {
			result.content += CONNECTION_ERROR;
		}
		
		return result.content;
    }

	/**
	 * Returns the properties of the shipment identified by shipmentid and all its specimens.
	 * 
	 * @param user_id the user's static id number, accessed with <code>HttpMethods.getSitekey()</code>
	 * @param key part of the user's current sitekey, generated by <code>HttpMethods.getSitekey()</code>
	 * @param shipment_id the unique id number of the shipment you want to view
	 * @return the XML output of 'shipment.pl' as a String
	 */
	public static String getShipment(String user_id, String key, String shipment_id)
	{
    	String data = "uid="+user_id+"&key="+key+"&sh_id="+shipment_id;
		HTTPData result = new HTTPData();
    	
    	try {
			result = HTTPRequest.get(SERVER_SCRIPTS_URL+"shipment.pl"+"?"+data);
		}
    	catch (IOException e) {
			result.content += CONNECTION_ERROR;
		}
    	
    	return result.content;
    }

	/**
	 * Alters the specimen identified by specimen_id
	 * 
	 * This is a POST method, so be careful of duplicate calls, they CAN and WILL alter state.
	 * 
	 * @param user_id the user's static id number, accessed with <code>HttpMethods.getSitekey()</code>
	 * @param key part of the user's current sitekey, generated by <code>HttpMethods.getSitekey()</code>
	 * @param specimen_id the unique id number of the specimen you want to alter
	 * @param intact
	 * @param missing
	 * @param damaged
	 * @return the XML output of 'receive.pl' as a String
	 */
	public static String setSpecimen(String user_id, String key, String specimen_id,
								String intact, String missing, String damaged)
	{
    	String data= "uid="+user_id+"&key="+key+"&sp_id="+specimen_id+
    				 "&i="+intact+"&m="+missing+"&d="+damaged;
		HTTPData result = new HTTPData();
		
		try {
			result= HTTPRequest.post(SERVER_SCRIPTS_URL+"receive.pl", data);
		}
		catch (IOException e) {
			result.content += CONNECTION_ERROR;
		}
		
		return result.content;
    }
	
	/**
	 * Clears the sitekey from the database, disallowing the user from making further
	 * requests without first calling getSitekey();
	 * 
	 * @param user_id the user's static id number, accessed with <code>HttpMethods.getSitekey()</code>
	 * @param key part of the user's current sitekey, generated by <code>HttpMethods.getSitekey()</code>
	 * @return the XML output of 'logout.pl' as a String
	 */
	public static String logout(String user_id, String key)
	{
    	String data = "uid="+user_id+"&key="+key;
    	HTTPData result = new HTTPData();
    	
    	try {
			result = HTTPRequest.get(SERVER_SCRIPTS_URL+"logout.pl"+"?"+data);
		}
    	catch (IOException e) {
			result.content += CONNECTION_ERROR;
		}
    	
    	return result.content;
    }

}
